﻿using System;

using System.Web.UI;
using System.Web;
using TMSPortal.UI.Resource;
using NetFramework;

namespace TMSPortal.UI
{
    public abstract class AjaxPageBase : Page
    {
        protected enum ResponseType
        {
            Json,
            Html
        }

        protected ResultShowType resultShowType;
        protected string resultBodyId;
        protected string resultBodyTitle;
        protected string resultBodyMessage;
        protected int resultBodyCode = 1;

        protected ResponseType ContextType
        {
            get;
            set;
        }

        protected override void OnLoad(EventArgs e)
        {
            object obj = AjaxResult(WebResourceValues.Action);
            if (ContextType == ResponseType.Json)
            {
                if (obj != null)
                {
                    ResultMessageObject body = View(resultShowType == ResultShowType.None ? ResultShowType.OK : resultShowType, obj, resultBodyCode);
                    string result = JsonHelper.ObjectToJson(body);
                    Response.Write(result);
                }
                else if (resultShowType == ResultShowType.OK)
                {
                    ResultMessage<ResultBody> body = View<ResultBody>(resultShowType, new ResultBody { Title = resultBodyTitle, Message = resultBodyMessage }, 1);
                    string result = JsonHelper.ObjectToJson(body);
                    Response.Write(result);
                }
                else
                {
                    ResultMessage<ResultBody> body = ErrorView(ResultShowType.Error, resultBodyId, resultBodyTitle, resultBodyMessage);
                    string result = JsonHelper.ObjectToJson(body);
                    Response.Write(result);
                }

                Response.ContentType = "application/json";
                Response.End();
            }
        }

        protected abstract object AjaxResult(string action);

        protected ResultMessage<ResultBody> ErrorView(ResultShowType showType, string id, string message)
        {
            return ErrorView(showType, id, string.Empty, message);
        }

        protected ResultMessage<ResultBody> ErrorView(ResultShowType showType, string id, string title, string message)
        {
            ResultMessage<ResultBody> view = new ResultMessage<ResultBody>();
            view.Code = 0;
            view.Description = showType.ToString();
            view.Body = new ResultBody { ID = id, Title=title, Message = message };

            return view;
        }

        protected ResultMessage<T> View<T>(ResultShowType showType, T body) where  T : class, new()
        {
            return View<T>(showType, body, 1);
        }

        protected ResultMessage<T> View<T>(ResultShowType showType, T body, int code) where T : class, new()
        {
            ResultMessage<T> view = new ResultMessage<T>();
            view.Code = code;
            view.Description = showType.ToString();
            view.Body = body;

            return view;
        }

        protected ResultMessageObject View(ResultShowType showType, object body, int code)
        {
            ResultMessageObject view = new ResultMessageObject();
            view.Code = code;
            view.Description = showType.ToString();
            view.Title = resultBodyTitle;
            view.Body = body;

            return view;
        }
    }
}
